package com.appleyk.mapping;


import com.appleyk.enums.GenderEnum;
import com.appleyk.model.hras.Employees;
import com.appleyk.model.hras.WorkHistory;
import com.appleyk.query.EmployeesQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmployeesMapping {

    @Resource
    private EmployeesQuery employeesQuery;

    /**
     * 获取员工职位信息
     *
     * @param name 员工姓名
     * @return 职位名称信息
     */
    public String getEmployeesPositionsName(String name) {
        List<String> employeesPositions = employeesQuery.getEmployeesPositions(name);
        return CollectionUtils.isNotEmpty(employeesPositions) ? String.join("\n", employeesPositions) : null;
    }

    /**
     * 查询 员工 性别
     *
     * @param reStrings reStrings
     * @return 性别
     */
    public String getEmployeesSex(List<String> reStrings) {
        return GenderEnum.getGenderName(employeesQuery.getEmployeesSex(reStrings.get(1)));
    }

    /**
     * 查询 员工 出生日期
     *
     * @param reStrings reStrings
     * @return 出生日期
     */
    public String getEmployeesBirthDate(List<String> reStrings) {
        return employeesQuery.getEmployeesBirthDate(reStrings.get(1));
    }

    /**
     * 查询 员工 身份证
     *
     * @param reStrings reStrings
     * @return 身份证
     */
    public String getEmployeesIdNumber(List<String> reStrings) {
        return employeesQuery.getEmployeesIdNumber(reStrings.get(1));
    }

    /**
     * 查询 员工 联系方式
     *
     * @param reStrings reStrings
     * @return 联系方式
     */
    public String getEmployeesContactInfo(List<String> reStrings) {
        return employeesQuery.getEmployeesContactInfo(reStrings.get(1));
    }

    /**
     * 查询 员工 家庭住址
     *
     * @param reStrings reStrings
     * @return 家庭住址
     */
    public String getEmployeesAddress(List<String> reStrings) {
        return employeesQuery.getEmployeesAddress(reStrings.get(1));
    }

    /**
     * 查询 员工 岗位详情
     *
     * @param reStrings reStrings
     * @return 岗位详情
     */
    public String getEmployeesPositionDetail(List<String> reStrings) {
        return employeesQuery.getEmployeesPositionDetail(reStrings.get(1));
    }

    /**
     * 查询 员工 部门
     *
     * @param reStrings reStrings
     * @return 部门
     */
    public String getEmployeesDepartments(List<String> reStrings) {
        return employeesQuery.getEmployeesDepartments(reStrings.get(1));
    }

    /**
     * 查询 员工 部门详情
     *
     * @param reStrings reStrings
     * @return 部门详情
     */
    public String getEmployeesDepartmentsDetail(List<String> reStrings) {
        return employeesQuery.getEmployeesDepartmentsDetail(reStrings.get(1));
    }

    /**
     * 查询 员工 员工数量
     *
     * @return 员工数量
     */
    public String getEmployeesCount() {
        return "公司总员工数为 " + employeesQuery.getEmployeesCount() + " 人";
    }

    /**
     * 查询 员工 男员工数量
     *
     * @return 男员工数量
     */
    public String getEmployeesMaleCount() {
        return "公司男员工数为 " + employeesQuery.getEmployeesGanderCount(GenderEnum.MALE.getCode()) + " 人";
    }

    /**
     * 查询 员工 女员工数量
     *
     * @return 女员工数量
     */
    public String getEmployeesFemaleCount() {
        return "公司女员工数为 " + employeesQuery.getEmployeesGanderCount(GenderEnum.FEMALE.getCode()) + " 人";
    }

    /**
     * 查询 大于 x 岁员工多少
     *
     * @param reStrings reStrings
     * @return 部门详情
     */
    public String getEmployeesThanX(List<String> reStrings) {
        String age = reStrings.get(2);
        if (StringUtils.isNotEmpty(age)) {
            age = age.contains(".") ? age.substring(0, age.indexOf(".")) : age;
            int x = Integer.parseInt(age);
            return "大于 " + x + " 岁的人有 " + employeesQuery.getEmployeesThanX(x) + " 人";
        }
        return null;
    }

    /**
     * 查询 小于 x 岁员工多少
     *
     * @param reStrings reStrings
     * @return 部门详情
     */
    public String getEmployeesLowX(List<String> reStrings) {
        String age = reStrings.get(2);
        if (StringUtils.isNotEmpty(age)) {
            age = age.contains(".") ? age.substring(0, age.indexOf(".")) : age;
            int x = Integer.parseInt(age);
            return "小于 " + x + " 岁的人有 " + employeesQuery.getEmployeesLowX(x) + " 人";
        }
        return null;
    }

    /**
     * 查询 员工 信息
     *
     * @param reStrings reStrings
     * @return 部门详情
     */
    public String getEmployeesInfo(List<String> reStrings) {
        Employees employees = employeesQuery.getEmployeesInfo(reStrings.get(1));
        return employees != null ? employees.getStr() : null;
    }

    /**
     * 查询 员工 加入公司时间
     *
     * @param reStrings reStrings
     * @return 加入公司时间
     */
    public String getEmployeesJoinTime(List<String> reStrings) {
        String name = reStrings.get(1);
        String joinTime = employeesQuery.getEmployeesJoinTime(name);
        return StringUtils.isNotEmpty(joinTime) ? name +"加入公司时间为：" + joinTime : null;
    }

    /**
     * 查询 员工 邮箱
     *
     * @param reStrings reStrings
     * @return 邮箱
     */
    public String getEmployeesEmail(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesEmail(name);
        return StringUtils.isNotEmpty(result) ? name +"邮箱为：" + result : null;
    }


    /**
     * 查询 员工 政治面貌
     *
     * @param reStrings reStrings
     * @return 政治面貌
     */
    public String getEmployeesHighestDegree(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesHighestDegree(name);
        return StringUtils.isNotEmpty(result) ? name +"政治面貌为：" + result : null;
    }

    /**
     * 查询 员工 最高学历
     *
     * @param reStrings reStrings
     * @return 最高学历
     */
    public String getEmployeesPoliticalProfile(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesPoliticalProfile(name);
        return StringUtils.isNotEmpty(result) ? name +"最高学历为：" + result : null;
    }

    /**
     * 查询 员工 籍贯
     *
     * @param reStrings reStrings
     * @return 籍贯
     */
    public String getEmployeesHometown(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesHometown(name);
        return StringUtils.isNotEmpty(result) ? name +"籍贯为：" + result : null;
    }

    /**
     * 查询 员工 紧急联系人
     *
     * @param reStrings reStrings
     * @return 紧急联系人
     */
    public String getEmployeesEmergencyContact(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesEmergencyContact(name);
        return StringUtils.isNotEmpty(result) ? name +"紧急联系人为：" + result : null;
    }

    /**
     * 查询 员工 紧急联系人电话
     *
     * @param reStrings reStrings
     * @return 紧急联系人电话
     */
    public String getEmployeesEmergencyContactPhone(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesEmergencyContactPhone(name);
        return StringUtils.isNotEmpty(result) ? name +"紧急联系人电话为：" + result : null;
    }

    /**
     * 查询 员工 毕业学校
     *
     * @param reStrings reStrings
     * @return 毕业学校
     */
    public String getEmployeesGraduateSchool(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesGraduateSchool(name);
        return StringUtils.isNotEmpty(result) ? name +"毕业学校为：" + result : null;
    }

    /**
     * 查询 员工 民族
     *
     * @param reStrings reStrings
     * @return 民族
     */
    public String getEmployeesNation(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesNation(name);
        return StringUtils.isNotEmpty(result) ? name +"民族为：" + result : null;
    }

    /**
     * 查询 员工 户籍所在地
     *
     * @param reStrings reStrings
     * @return 户籍所在地
     */
    public String getEmployeesHousehold(List<String> reStrings) {
        String name = reStrings.get(1);
        String result = employeesQuery.getEmployeesHousehold(name);
        return StringUtils.isNotEmpty(result) ? name +"户籍所在地为：" + result : null;
    }

    /**
     * 查询 员工 证书
     *
     * @param reStrings reStrings
     * @return 证书
     */
    public String getEmployeesCertificate(List<String> reStrings) {
        String name = reStrings.get(1);
        List<String> certificates = employeesQuery.getEmployeesCertificate(name);
        return CollectionUtils.isNotEmpty(certificates)? String.join("\n",certificates) : null;
    }

    /**
     * 查询 员工 工作经历
     *
     * @param reStrings reStrings
     * @return 工作经历
     */
    public String getEmployeesWorkHistory(List<String> reStrings) {
        String name = reStrings.get(1);
        List<WorkHistory> histories = employeesQuery.getEmployeesWorkHistory(name);
        if(CollectionUtils.isNotEmpty(histories)){
            return histories.stream().map(WorkHistory::getStr).collect(Collectors.joining("\n"));
        }
        return null;
    }





}
